# -*- coding: utf-8 -*-
"""
告警相关的工具函数
"""
from typing import Optional
from common.base_enums import AlarmType


def get_alarm_type_label(alarm_type: str) -> str:
    """
    获取告警类型的中文标签

    Args:
        alarm_type: 告警类型编码

    Returns:
        str: 中文标签，如果找不到则返回原值
    """
    try:
        # 尝试从枚举中获取标签
        alarm_enum = AlarmType(alarm_type)
        return alarm_enum.label()
    except (ValueError, AttributeError):
        # 如果枚举中没有找到，返回原值
        return alarm_type


def format_alarm_message(meter_code: str, alarm_type: str, action: str = "告警") -> str:
    """
    格式化告警消息

    Args:
        meter_code: 电表编码
        alarm_type: 告警类型编码
        action: 动作类型（告警、恢复等）

    Returns:
        str: 格式化后的告警消息
    """
    # 获取告警类型的中文标签
    alarm_label = get_alarm_type_label(alarm_type)

    # 对于特殊的电表类型+告警组合，进行特殊处理
    if "-" in alarm_type:
        # 处理类似 THREE-TAMPER, SINGLE-TAMPER 的情况
        parts = alarm_type.split("-")
        if len(parts) == 2:
            meter_type_part, alarm_part = parts
            # 只使用告警部分来获取中文标签
            clean_alarm_label = get_alarm_type_label(alarm_part)
            return f"{meter_code}{action}: {clean_alarm_label}"
    
    return f"{meter_code}{action}: {alarm_label}"


def format_work_order_title(meter_code: str, alarm_type: str) -> str:
    """
    格式化工单标题

    Args:
        meter_code: 电表编码
        alarm_type: 告警类型编码

    Returns:
        str: 格式化后的工单标题
    """
    # 获取告警类型的中文标签
    alarm_label = get_alarm_type_label(alarm_type)
    
    # 对于特殊的电表类型+告警组合，进行特殊处理
    if "-" in alarm_type:
        # 处理类似 THREE-TAMPER, SINGLE-TAMPER 的情况
        parts = alarm_type.split("-")
        if len(parts) == 2:
            meter_type_part, alarm_part = parts
            # 只使用告警部分来获取中文标签
            clean_alarm_label = get_alarm_type_label(alarm_part)
            return f"电表{meter_code}{clean_alarm_label}"
    
    return f"电表{meter_code}{alarm_label}"